import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Test().start();

	}

	public void start() {

		String sourceName = "truc";
		String sourcePath = "./dynJavaSources/" + sourceName + ".java";
		Dynamizer dynam = new Dynamizer(sourcePath);

		dynam.append("public class truc extends ImplementMe{" + "public void doSomething(){" + "System.out.println(\"I do something!!!\");" + "}" + "}");
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

		int result = compiler.run(null, null, null, sourcePath);
		System.out.println("Compile result code = " + result);
		
		String className = sourceName;
		loadClass(className);
	}

	public void loadClass(String classeName) {
		try {

			File root = new File("./dynJavaSources/");
			URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
			Class<? extends ImplementMe> myclass = (Class<? extends ImplementMe>) Class.forName(classeName, true, classLoader);

			ImplementMe object = myclass.newInstance();
			object.doSomething();

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
